//
//  ClassListViewController.m
//  moon
//
//  Created by beyself on 16/6/15.
//  Copyright © 2016年 beyself. All rights reserved.
//
#import "Utils.h"
#import "ClassListViewController.h"
#import "CourseViewController.h"

@interface ClassListViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation ClassListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"班级列表";
    
    _mArray = [[NSMutableArray alloc] init];
    [_mArray setArray:APPDELEGATE.mMemberInfo[@"classes"]];
    
    UITableView* tableview = [[UITableView alloc] init];
    tableview.backgroundColor = [UIColor clearColor];
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableview.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:tableview];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _mArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
        cell.backgroundColor = WHITE;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        UIImageView* imageview = [[UIImageView alloc] init];
        imageview.frame = CGRectMake(10, 10, 44, 44);
        imageview.layer.cornerRadius = 22;
        imageview.layer.masksToBounds = YES;
        imageview.image = [UIImage imageNamed:@"ic_class"];
        [cell addSubview:imageview];
        
        UILabel* label = [[UILabel alloc] init];
        label.frame = CGRectMake(64, 10, SCREEN_WIDTH - 100, 22);
        label.tag = 1000;
        label.textColor = BLUE;
        label.font = FONT_14;
        [cell addSubview:label];
        
        label = [[UILabel alloc] init];
        label.frame = CGRectMake(64, 32, SCREEN_WIDTH - 100, 22);
        label.tag = 1001;
        label.textColor = BLUE;
        label.font = FONT_14;
        [cell addSubview:label];
    }

    NSDictionary* item = _mArray[indexPath.section];
    ((UILabel*)[cell viewWithTag:1000]).text = item[@"school_name"];
    
    NSString* className = item[@"class_alias"];
    if([Utils isStringNullOrEmpty:className]) className = item[@"class_name"];
    ((UILabel*)[cell viewWithTag:1001]).text = className;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary* item = _mArray[indexPath.section];
    NSString* className = item[@"class_alias"];
    if([Utils isStringNullOrEmpty:className]) className = item[@"class_name"];

    CourseViewController* subview = [[CourseViewController alloc] init];
    subview.className = className;
    subview.classId = [item[@"id"] intValue];
    [self.navigationController pushViewController:subview animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
